FreeTask = {
	//建立免費案件
	create:function(form){
		if(form.fromLang.value == form.toLang.value){
			Message.show("msg-wrong-format", 400, "格式錯誤", "翻譯語言的來源與目的不得相同", Message.BUTTON_OK);
			return;
		}
		if(form.content.value == ""){
			Message.show("msg-wrong-format", 400, "格式錯誤", "請填寫翻譯原文", Message.BUTTON_OK);
			return;
		}
		if(form.content.value.length > 200){
			Message.show("msg-wrong-format", 400, "格式錯誤", "翻譯原文不得超過 200 個字元", Message.BUTTON_OK);
			return;
		}
		if(!form.agree.checked){
			Message.show("msg-wrong-format", 400, "錯誤", "請先點擊同意注意事項", Message.BUTTON_OK);
			return;
		}
		
		var send = function(){
			Message.show("msg-task-creating", 400, "案件建立中", Ajax.loadingImg);
			Ajax.sendRequest(
				"post", "/exe/member/CreateFreeTask", 
				"from_lan=" + form.fromLang.value + "&to_lan=" + form.toLang.value
					+ "&content=" + encodeURIComponent(form.content.value), 
				true, 
				function(req){
					Message.hide("msg-task-creating");
					var result = req.getResponseHeader("result");
					if(result == "true"){
						Message.show(
							"msg-task-result", 400, "建立成功", "免費案件已成功建立", 
							new Message.Button("確定", function(){document.location.href='/free-task/free-task-list.jsp';})
						);
					}else{
						Message.show("msg-task-result", 400, "建立失敗", "請稍後重試或聯絡客服", Message.BUTTON_OK);
					}
				}
			);
		}
		Message.show(
			"msg-task-create-confirm", 400, "確認建立免費案件", 
			"是否確定建立免費案件？<br/>一旦建立免費案件，案件將無法撤回。",
			new Message.Button("確定", send), Message.BUTTON_CANCEL
		);
	},
	//翻譯免費案件
	response:function(form){
		if(form.content.value == ""){
			Message.show("msg-wrong-format", 400, "格式錯誤", "請填寫翻譯內容", Message.BUTTON_OK);
			return;
		}
		if(form.tid.value == ""){
			Message.show("msg-wrong-format", 400, "系統錯誤", "請稍後重試或聯絡客服", Message.BUTTON_OK);
			return;
		}
		
		var send = function(){
			Message.show("msg-free-task-responding", 400, "系統處理中", Ajax.loadingImg);
			Ajax.sendRequest(
				"post", "/exe/member/CreateFreeResponse", 
				"id=" + form.tid.value + "&content=" + encodeURIComponent(form.content.value), true, 
				function(req){
					Message.hide("msg-free-task-responding");
					var result = req.getResponseHeader("result");
					if(result == 1){
						Message.show(
							"msg-free-task-response-result", 400, "處理成功", "您的翻譯已成功送出", Button.buttonReload);
					}else{
						Message.show(
							"msg-free-task-response-result", 400, "處理失敗", "請稍後重試或聯絡客服", Message.BUTTON_OK);
					}
				}
			);
		}
		
		Message.show(
			"msg-free-task-confirm", 400, "確認送出翻譯", 
			"是否確定送出翻譯？<br/>翻譯譯文一旦送出，將無法再進行更改。", 
			new Message.Button("確定", send), Message.BUTTON_CANCEL
		);
	},
	//選擇最佳翻譯
	pickResponse:function(responseId, translatorId){
		if(!responseId || !translatorId){
			return;
		}
		
		var pick = function(){
			Message.show("msg-pick-response-processing", 400, "系統處理中", Ajax.loadingImg);
			Ajax.sendRequest(
				"post", "/exe/member/PickFreeResponse", "id=" + responseId + "&translator=" + translatorId, true, 
				function(req){
					Message.hide("msg-pick-response-processing");
					Message.show(
						"msg-pick-response-result", 400, "選擇成功", 
						"您所選擇的翻譯內容已成為最佳翻譯。<br/>此案件狀態變更為已結案。", 
						Button.buttonReload
					);
				}
			);
		}
		Message.show(
			"msg-pick-response-confirm", 500, "確認選擇最佳翻譯", 
			"您確定要選擇此翻譯為最佳翻譯嗎？<br/>一旦選擇後，案件狀態將變更為已結案，且選擇將無法更改。",
			new Message.Button("確定", pick), Message.BUTTON_CANCEL
		);
	},
	//展開／隱藏免費案件翻譯列表的翻譯
	showResponse:function(expand, id){
		var response = document.getElementById("task-response-" + id);
		response.style.display = "table-row";
		expand.innerHTML = expand.innerHTML.replace("▼", "▲").replace("▼", "▲").replace("展開", "隱藏");
		expand.onclick = function(){
			FreeTask.hideResponse(this, this.id);
		}
	},
	hideResponse:function(expand, id){
		var response = document.getElementById("task-response-" + id);
		response.style.display = "none";
		expand.innerHTML = expand.innerHTML.replace("▲", "▼").replace("▲", "▼").replace("隱藏", "展開");
		expand.onclick = function(){
			FreeTask.showResponse(this, this.id);
		}
	}
};